<?php
namespace TwinPeaks\Core;

class CFilter
{
	use TSingleton;

	/**
	 * @var array
	 */
	private $params = [];
	
	/**
	 * @return mixed
	 */
	final function __call($name, $args)
	{
		$prefix = substr($name, 0, 3);
		$tail = strtolower($name[3]) . substr($name, 4);
		$paramName = strtolower(preg_replace('~([A-Z])~', '_$1', $tail));
		
		if (strcasecmp('get', $prefix) == 0)
		{
			if (($param = $this->findParamByName($paramName)) != null)
				return $param->getValue();
			else	
				CCommonException::filterNoParamDef($paramName)->_throw();				
		}
		else if (strcasecmp('has', $prefix) == 0)
		{
			return $this->findParamByName($paramName) ? true : false;
		}
	}
	
	/**
	 * @return array|null
	 */
	function findParamByName($paramName)
	{
		foreach ($this->params as $param)
		{
			if ($param->getName() == $paramName)
				return $param;
		}
		return null;
	}
	
	/**
	 * @return void
	 */
	final function addParam(CFilterParam $param)
	{
		$this->params[] = $param;
	}
}
